import os
import shutil
import tempfile

def clean_system_temp():
    # 获取系统临时文件夹路径
    temp_folder = tempfile.gettempdir()
    try:
        # 遍历临时文件夹下的所有文件和子文件夹
        for root, dirs, files in os.walk(temp_folder):
            for file in files:
                file_path = os.path.join(root, file)
                try:
                    # 删除文件
                    os.remove(file_path)
                    print(f"已删除文件: {file_path}")
                except Exception as e:
                    print(f"删除文件 {file_path} 时出错: {e}")
            for dir in dirs:
                dir_path = os.path.join(root, dir)
                try:
                    # 删除文件夹
                    shutil.rmtree(dir_path)
                    print(f"已删除文件夹: {dir_path}")
                except Exception as e:
                    print(f"删除文件夹 {dir_path} 时出错: {e}")
    except Exception as e:
        print(f"清理系统临时文件夹时出错: {e}")

clean_system_temp()